const { BrowserWindow, app, ipcMain, screen, shell } = require("electron");
const path = require("path");

// 获取指定的屏幕
function getScreenContent() {
  const displays = screen.getAllDisplays();
  if (displays.length > 2) {
    const targetDisplay = displays[2];
    const workAreaSize = targetDisplay.workArea;
    return workAreaSize;
  } else {
    return displays.workArea;
  }
}

const createWin = () => {
  console.log("getScreenContent", getScreenContent());
  const workArea = getScreenContent();
  const windowWidth = 800;
  const windowHeight = 600;

  // 计算窗口在目标屏幕上居中时的 x 和 y 坐标
  const x = workArea.x + (workArea.width - windowWidth) / 2;
  const y = workArea.y + (workArea.height - windowHeight) / 2;

  const win = new BrowserWindow({
    width: windowWidth,
    height: windowHeight,
    //  alwaysOnTop: true,
    x: x,
    y: y,
    // center: true,
    webPreferences: {
      preload: path.resolve(__dirname, "../preload.js"),
    },
  });

  win.webContents.openDevTools();
  // setTimeout(() => {
  //   //  win.center();
  //   win.setBounds(
  //     {
  //       x: 2000,
  //       y: 100,
  //       width: 600,
  //       height: 600,
  //     },
  //     true
  //   );
  // }, 2000);
  // win.loadFile("./index.html");
  // win.loadFile(path.resolve(__dirname, "../indexMenu.html"));
  win.loadFile(path.resolve(__dirname, "../indexDialog.html"));
  // win.loadURL("http://localhost:5173/");
  // 打开url 在浏览器上打开
  win.webContents.setWindowOpenHandler((details) => {
    shell.openExternal(details.url);
    return { action: "deny" };
  });
  return win;
};

module.exports = {
  createWin,
};
